package com.demo.services.impl;

import com.demo.common.AppResult;
import com.demo.common.ResultCode;
import com.demo.dao.BoardMapper;
import com.demo.exception.ApplicationException;
import com.demo.model.Board;
import com.demo.services.IBoardService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author quan
 * @Description 实现Service接口中的方法
 * @Date 2023/8/11 21:54
 */

@Slf4j
@Service
public class BoardServiceImpl implements IBoardService {
    @Resource
    private BoardMapper boardMapper;


    @Override
    public List<Board> selectByNum(Integer num) {
        //1、参数校验
        if(num == null || num<0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2、调用dao包下的数据库查询方法，获取结果
        List<Board> result = boardMapper.selectByNum(9);
        //3、返回结果
        return result;
    }

    @Override
    public Board selectById(Long id) {
        //1-非空校验
        if(id == null || id <=0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2-调用mapper
        Board result = boardMapper.selectByPrimaryKey(id);
        //3-返回结果
        return result;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        //1-参数校验Id
        if(id == null || id<=0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2-通过id查询版块
        Board board = selectById(id);
        //3-校验版块是否存在
        if(board == null){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //4-构建要更新的版块对象
        Board updateBoard = new Board();
        updateBoard.setId(board.getId()); //版块Id
        updateBoard.setArticleCount(board.getArticleCount()+1); //帖子数+1
        updateBoard.setUpdateTime(new Date()); //设置更新时间
        //5-更新版块对象
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //6-对结果校验row
        if(row != 1){
            //打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        //不抛出异常说明执行成功
    }

    @Override
    public void subOneArticleCountById(Long id) {
        //1-参数校验Id
        if(id == null || id<=0){
            //打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2-通过id查询版块
        Board board = selectById(id);
        //3-校验版块是否存在
        if(board == null){
            //打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        //4-构建要更新的版块对象
        Board updateBoard = new Board();
        updateBoard.setId(board.getId()); //版块Id
        updateBoard.setArticleCount(board.getArticleCount()-1); //帖子数-1
        updateBoard.setUpdateTime(new Date()); //设置更新时间
        //5-更新版块对象
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        //6-对结果校验row
        if(row != 1){
            //打印日志
            log.warn(ResultCode.ERROR_SERVICES.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        //不抛出异常说明执行成功
    }

}
